$(document).ready(function () {
    $("#myCarousel").carousel({
        interval: 5000,
        wrap: true
    });
    var getUrl = "http://api.shur.cn/v1/activities/1?access_token=e3f9595e4559bceeda28514ba624b7ca290229c1&location=30.620076%2C104.067221";

    if (checkUrl(getUrl)) {
        get(getUrl, 'GET', {
            loading: function () {
                console.log('加载中。。。');
            },
            success: function (data) {
                console.log('回调-data:' + data);
                init(data);
            },
            error: function () {
                console.log('加载失败');
            }
        });
    }

    function init(data) {
        var attachments = data.attachments;
        console.log(attachments);
        if (attachments != "" && attachments != null && attachments != undefined) {
            $.each(attachments, function (index, item) {
                var is_video = item.is_video;
                if (is_video == 0) {
                    var url = item.url;
                    var a = '';
                    var b = '';
                    if (index == 0) {
                        a = '<li data-target="#myCarousel" data-slide-to=' + index + ' class="active"></li>';
                        b = '<div class="item active">\n' +
                            '            <img src=' + url + '>\n' +
                            '        </div>';
                    } else {
                        a = '<li data-target="#myCarousel" data-slide-to=' + index + '></li>';
                        b = '<div class="item">\n' +
                            '            <img src=' + url + '>\n' +
                            '        </div>';
                    }
                    $('#inds').append(a);
                    $('#imgs').append(b);

                }
            });

        }
        //标题
        var title = data.title;
        console.log("title:" + title);
        $('#activity-title').text(title);
        //副标题
        var sub_title = data.sub_title;
        console.log(sub_title);
        $('#activity-sub-title').text(sub_title);
        //金额
        var original_price = data.original_price;
        console.log(original_price);
        $('#activity-money').text('￥' + original_price);

        //套餐列表
        var prices = data.prices;
        if (prices != "" && prices != null && prices != undefined) {
            //prices是个数组
            $.each(prices, function (index, item) {

                //套餐价格
                var price = item.price;
                var title = item.title;
                var introduce = item.introduce;
                var c = '<div class="set-meal-content" >\n' +
                    '    <div class="set-meal-top">\n' +
                    '\n' +
                    '        <p class="text set-meal-title s-inline-block">' + title + '</p>\n' +
                    '        <p class="set-meal-money s-inline-block">' + "￥" + price + '</p>\n' +
                    '    </div>\n' +
                    '    <div class="set-meal">\n' +
                    '        <p class="set-meal-time">\n' +
                    '            套餐说明\n' +
                    '        </p>\n' +
                    '        <p class="set-meal-btn">\n' +
                    '            立即购买\n' +
                    '        </p>\n' +
                    '        <div class="set-meal-detail s-block">\n' +
                    '            ' + introduce + '\n' +
                    '        </div>\n' +
                    '    </div>\n' +
                    '</div>';


                // $('.set-meal').unbind('click').delegate('.set-meal-time','click',function () {
                //
                //      $(this).siblings('.set-meal-detail').toggle();
                //
                //     //alert(index+'-'+$(this).siblings('.set-meal-detail').text());
                //
                // });
                $('#set-meal-content').append(c);

            });
            $('.set-meal').unbind('click').delegate('.set-meal-time', 'click', function () {

                $(this).siblings('.set-meal-detail').toggle();

                console.log($(this).siblings('.set-meal-detail').text());

            });

        }
        //活动地址
        var address = data.address;
        console.log("address:" + address);
        if (address != "" && address != null && address != undefined) {

            var name = address[0].name;
            var detail = address[0].detail;
            var distance = address[0].distance;
            var e = '<div class="set-meal-address">\n' +
                '        <div class="set-meal-address-top">\n' +
                '            <p class="text">\n' +
                '                ' + name + '\n' +
                '            </p>\n' +
                '            <p class="text-0-5">\n' +
                '                ' + detail + '\n' +
                '            </p>\n' +
                '        </div>\n' +
                '        <p class="distance">' + distance + "米" + '</p>\n' +
                '    </div>';

            $('#set-meal-address').append(e);
            if (address.length > 1) {
                var f = ' <div id="set-meal-address-mpre">\n' +
                    '        更多地址>>\n' +
                    '    </div>';
                $('#set-meal-address').append(f);
            }


        }

        //主办方
        var partner = data.partner;
        if (partner != "" && partner != null && partner != undefined) {
            //logo
            var logo_url = partner.logo_url;
            //主办方名称
            var name = partner.name;
            console.log('logo_url:'+logo_url);
            //主办方简介
            var brief = partner.brief;
            $('#initiator-logo').attr('src', logo_url);
            $('#initiator-address').text(name);
            $('#initiator-information').text(brief);
        }
        //活动详情
        var description = data.description;
        //海报
        var poster_url = data.poster_url;
        //取消条款
        var cancellation = data.cancellation;

        console.log(poster_url);

        $('#activity-introduction-information').html(description);

        $('#activity-introduction-poster').attr('src', poster_url);

        $('#activity-introduction-cancellation').text(cancellation);
    }
    $('#btn-up-app').click(function () {
        console.log("app-btn-click");
        // var scheme = "scheme://host/pathPrefix?id=44";
        var scheme = "scheme://host/pathPrefix";
        startApp(scheme);
    });
});